//
//  CountryManager.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/2/1.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "CountryManager.h"


@implementation CountryManager

+ (instancetype)sharedManager {
    
    static CountryManager *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[CountryManager alloc] init];
        
    });
    return _sharedManager;
}

- (NSArray<Country *> *)onlinecountry {
    
    return [_onlinecountry sortedArrayUsingComparator:^NSComparisonResult(Country * obj1, Country * obj2) {
        
        return [[IGUtil firstCharactor:obj1.smartName] compare:[IGUtil firstCharactor:obj2.smartName]];
    }];
}

- (NSArray *)getOnliecitys {
    
    NSMutableArray *citys = [NSMutableArray array];
    
    for (Country *country in self.onlinecountry) {
        [citys addObjectsFromArray:country.cities];
    }
    
    
    
    return citys;
}

- (NSArray *)getNearbyCity:(City *)city {
    
    if (!city) return nil;
    
    NSString *searchString = city.continent;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"continent == %@", searchString];
    NSArray *filteredArray = [[self getOnliecitys] filteredArrayUsingPredicate:predicate];
    
    CLLocationCoordinate2D myCoordinate = CLLocationCoordinate2DMake(city.lat.floatValue, city.lng.floatValue);
    
    NSArray *nearArray = [filteredArray sortedArrayUsingComparator:^NSComparisonResult(City * obj1, City * obj2) {
        
        if ([obj1.cid isEqualToNumber:city.cid]) obj1.isGPS = YES;
        if ([obj2.cid isEqualToNumber:city.cid]) obj2.isGPS = YES;
        
        CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(obj1.lat.floatValue, obj1.lng.floatValue);
        
        CGFloat distance1 = [IGUtil getDistanceFromCoordinate:coordinate1 toCoordinate:myCoordinate];
        
        CLLocationCoordinate2D coordinate2 = CLLocationCoordinate2DMake(obj2.lat.floatValue, obj2.lng.floatValue);
        
        CGFloat distance2 = [IGUtil getDistanceFromCoordinate:coordinate2 toCoordinate:myCoordinate];
        
        return distance1 >= distance2;
        
    }];
    
    if (nearArray.count > 9) {
        
        nearArray = [nearArray subarrayWithRange:NSMakeRange(0, 9)];
    }
    
    return nearArray;
}

- (NSArray *)getCitysMatch:searchText {
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"keywords CONTAINS [cd] %@", searchText];
    return [[self getOnliecitys] filteredArrayUsingPredicate:predicate];
}

- (NSArray *)getAllCountryIdxArray {
    
    NSMutableArray *idxArray = [NSMutableArray array];
    
    for (Country *country in self.allcountry) {
        
        NSString *idx = [IGUtil firstCharactor:country.smartName];
        
        if (![idxArray containsObject:idx]) {
            
            [idxArray addObject:idx];
        }
    }
    
    return [idxArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return [obj1 compare:obj2];
    }];
}

- (NSDictionary *)getAllCountryIdxToCitiesDict {
    
    NSMutableDictionary *idxDict = [NSMutableDictionary dictionary];
    
    for (Country *country in self.allcountry) {
        
        NSString *idx = [IGUtil firstCharactor:country.smartName];
        
        NSMutableArray *array = idxDict[idx];
        
        if (!array) {
            
            array = [NSMutableArray array];
            [idxDict setObject:array forKey:idx];
        }
        
        [array addObject:country];
    }
    
    return idxDict;
}

- (NSArray *)saveRateArray {
    
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"kSaveRateArray"];
    
    if (!array || array.count == 0) {
        
        
        array = @[@"CNY",@"USD",@"EUR"];
    }
    
    return array;
}

- (void)setSaveRateArray:(NSArray *)saveRateArray {

    
    [[NSUserDefaults standardUserDefaults] setObject:saveRateArray forKey:@"kSaveRateArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}



@end
